<template>
  <div>
    <template>
      <Card style="width:850px;margin: auto;">
        <Form :model="formLeft" label-position="left" :rules="validateLibForm" :label-width="100" style="width: 300px;margin: auto;">

          <FormItem label="产品代码" prop="productName" >
            <Select v-model="formLeft.productName" filterable>
              <Option v-for="item in productList" :value="item.name">{{ item.name }}</Option>
            </Select>
          </FormItem>

          <FormItem label="订单号:"  prop="orderNo">
            <Input v-model="formLeft.orderNo" :maxlength="20"></Input>
          </FormItem>

          <FormItem label="计划数量:" prop="planQuantity">
            <InputNumber v-model="formLeft.planQuantity" :maxlength="10" style="width: 200px"/>
          </FormItem>


          <FormItem label="计划日期" prop="startTime" style="width: 500px">
            <DatePicker type="date"
                        :value="formLeft.startTime"
                        @on-change="dateplanDate($event,'startTime')"
                        style="width: 200px">

            </DatePicker>
          </FormItem>

          <FormItem label="交货日期" prop="endTime" style="width: 500px">
            <DatePicker type="date"
                        :value="formLeft.endTime"
                        @on-change="datedeliverDate($event,'endTime')"
                        style="width: 200px">

            </DatePicker>
          </FormItem>


          <row type="flex" justify="center">
            <i-col span="20">

            </i-col>
            <i-col span="30">
              <Button type="primary" @click="OrderInsert">添加</Button>
              <Button type="success" style="margin-left: 160px" @click="back">返回</Button>
            </i-col>
          </row>

        </Form>
      </Card>
    </template>
  </div>
</template>

<script>
  import {OrderInsert, ProductFindAll} from "../../../api/data"

  export default {
    data() {
      return {
        page: 1,//page 第几页
        pageSize: 10,//pageSize:每页几条数据
        total: 0,
        productList: [],
        formLeft: {
          productName: '',
          orderNo: '',
          planQuantity: 0,
          startTime: '',
          endTime: '',
          priority: ''
        },
        validateLibForm: {
          productName: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          orderNo: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          quantity: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          startTime: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          endTime: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
        }
      }
    },
    mounted() {
      this.ProductFindAll();
    },
    methods: {
      dateplanDate(value, key) {
        this.formLeft[key] = value
      },
      datedeliverDate(value, key) {
        this.formLeft[key] = value
      },
      OrderInsert() {
        if (this.formLeft.productName !== null && this.formLeft.productName !== "") {
          if (this.formLeft.orderNo !== null && this.formLeft.orderNo !== "") {
            if (this.formLeft.planQuantity !== null && this.formLeft.planQuantity !== "") {
              if (this.formLeft.startTime !== null && this.formLeft.startTime !== "") {
                if (this.formLeft.endTime !== null && this.formLeft.endTime !== "") {
                    OrderInsert(this.formLeft).then(res => {
                      this.$Message.success("添加成功");
                      this.$router.go(-1)
                    })
                } else {
                  this.$Message.error("交货日期不能为空");
                }
              } else {
                this.$Message.error("计划日期不能为空");
              }
            } else {
              this.$Message.error("数量不能为空");
            }
          } else {
            this.$Message.error("订单号不能为空");
          }
        } else {
          this.$Message.error("产品代码不能为空");
        }
      },
      ProductFindAll() {
        var data = {
          page: this.page,
          pageSize: this.pageSize,
        };
        ProductFindAll(data).then(data => {
          if (data.data.code == 0) {
            this.productList = data.data.data.list;
          }
        })
      },
      back() {
        this.$router.go(-1);
      },

    }
  }
</script>


